//
//  HXCatagoryCollectionViewCell.m
//  GiftSay
//
//  Created by laohu on 16/1/9.
//  Copyright © 2016年 liyinkai. All rights reserved.
//

#import "HXCatagoryCollectionViewCell.h"
#import "HXLabelVerticalAlignment.h"
@implementation HXCatagoryCollectionViewCell
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self createView];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refresh) name:@"NightModeOpen" object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refresh) name:@"NightModeOff" object:nil];
    }
    return self;
}
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)setModel:(HXCatagorySecondModel *)model {
    _model = model;
    [self.image setImageWithURL:[NSURL URLWithString:_model.icon_url] placeholder:nil];
    self.label.text = _model.name;
    
}
- (void)refresh {
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"NightMode"] == YES) {
        self.label.textColor = [UIColor whiteColor];
    } else {
        self.label.textColor = [UIColor blackColor];
    }
}
- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes {
    self.image.frame = CGRectMake(0, 0, self.width, self.width);
    self.label.frame = CGRectMake(0, self.image.bottom, self.width, self.height - self.width);
}

- (void)createView {
    self.image = [[UIImageView alloc] initWithFrame:CGRectZero];
    self.image.layer.cornerRadius = self.width / 2;
//    self.image.backgroundColor = [UIColor greenColor];
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"NightMode"] == YES) {
        self.image.backgroundColor = kNightModeColor;
    } else {
        self.image.backgroundColor = [UIColor whiteColor];
    }
    [self addSubview:_image];
    
    self.label = [[HXLabelVerticalAlignment alloc] initWithFrame:CGRectZero];
    [self.label setVerticalAlignment:VerticalAlignmentTop];
    self.label.font = [UIFont systemFontOfSize:13 * SCREEN_WIDTH];
//    self.label.backgroundColor = [UIColor yellowColor];
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"NightMode"] == YES) {
        self.label.textColor = [UIColor whiteColor];
    } else {
        self.label.textColor = [UIColor blackColor];
    }
    self.label.textAlignment = NSTextAlignmentCenter;
    [self addSubview:_label];
    
}
@end
